/* 
	@license.copyright@
	@license.licenseInfo@
	$Id: BaseTask.java 6 2007-05-09 03:43:46Z denis.riabtchik $
*/

package jgrouse.jgtools;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileList;
import org.apache.tools.ant.types.FileSet;

public abstract class BaseTask extends Task
{
	private Project m_project;
	private List m_fileSets;
	private String m_output;
	
	public BaseTask()
	{
		m_fileSets = new ArrayList();
	}
	
	public Project getProject()
	{
		return m_project;
	}
	
	public void setProject(Project proj)
	{
		m_project = proj;
	}
	
	public void addFileset(FileSet pFileSet)
	{
		m_fileSets.add(pFileSet);
	}
	
	public void addFilelist(FileList pFileList)
	{
		m_fileSets.add(pFileList);
	}

	public String getOutput()
	{
		return m_output;
	}

	public void setOutput(String pOutput)
	{
		m_output = pOutput;
	}
	
	protected List getFiles()
	{
		List result = new ArrayList();
		for (Iterator iter = m_fileSets.iterator(); iter.hasNext();)
		{
			Object obj = iter.next();
			File dir = null;
			String[] files = null;
			if (obj instanceof FileSet)
			{
				FileSet fset = (FileSet) obj;
				dir = fset.getDir();
				DirectoryScanner ds = fset.getDirectoryScanner(getProject());
				files = ds.getIncludedFiles();				
			}
			else
			if (obj instanceof FileList)
			{
				FileList fl = (FileList) obj;	
				dir = fl.getDir(getProject());
				files = fl.getFiles(getProject());
			}
			for (int i = 0; i < files.length; i++)
			{
				result.add(new FileInfo(dir, files[i]));
			}
		}
		return result;
	}
	
	public static class FileInfo
	{
		public File dir;
		public String file;
		
		public FileInfo(File pDir, String pFile)
		{
			dir = pDir;
			file = pFile;
		}
	}
}
